/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package administrador;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;

/**
 *
 * @author daniel1
 */
public class imprimeErradas {
    Archivo facturaTXT;
    String archNom;
    public imprimeErradas() {
        facturaTXT = new Archivo();
        String fechaAct=new java.sql.Date(System.currentTimeMillis()).toString().replaceAll("-","");
        archNom = "totalErradas/reporte.txt";
        facturaTXT.crearArchivo(archNom);
    }
    public void escriboFact(String dataIns) {
        facturaTXT.escriboLineaArchivo(archNom, dataIns);
    }
    public void realizaImprecion() {


        try {
            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);

            PrintService service = printService[this.sacoImpresora(printService)];

            // Open the image file
            InputStream is = new BufferedInputStream(new FileInputStream(archNom));
            // Create the print job
            DocPrintJob job = service.createPrintJob();
            DocAttributeSet das = new HashDocAttributeSet();
            Doc doc = new SimpleDoc(is, flavor, das);

            // Monitor print job events
            PrintJobWatcher pjDone = new PrintJobWatcher(job);

            // Print it
            job.print(doc, pras);

            // Wait for the print job to be done
            pjDone.waitForDone();

            // It is now safe to close the input stream
            is.close();
        } catch (PrintException e) {
        } catch (IOException e) {
        }
    }

    public int sacoImpresora(PrintService Impresoras[]) {
        //Busco la Impresora:
        int Epson = 0;
        int numb = 0;
        while (numb < Impresoras.length) {
            if (Impresoras[numb].getName().toString().startsWith("EPSON")) {
                Epson = numb;
            }
            numb++;
        }
        return Epson;
    }

    public Archivo getArchivo() {
        return facturaTXT;
    }

    public void setArchivo(Archivo archivocp) {
        facturaTXT = archivocp;
    }
}